package dr.joketalker.controller;

import java.util.LinkedList;
import java.util.List;
import java.util.Observable;


import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import dr.joketalker.R;
import dr.joketalker.controller.view.ListRowAdapterSearch;
import dr.joketalker.controller.view.OneLineView;
import dr.joketalker.entity.Joke;
import dr.joketalker.model.SearchModel;



public class SearchController extends MyController implements OnItemClickListener, OnScrollListener {

	protected ListView lvJoke;
	protected Button btnSearch;
	protected ListRowAdapterSearch lraJoke;

	
	protected SearchModel model = SearchModel.GetInstance();
	private int firstVisibleItem;
	private int visibleItemCount;
	private int totalItemCount;

	
	 @Override
	public void onCreate(Bundle savedInstanceState) {
	 
	        super.onCreate(savedInstanceState);
	        
	        model.addObserver(this);
	        setContentView(R.layout.search);

       
	        btnSearch 		= (Button)findViewById(R.id.btnSearch);
	        lvJoke 			= (ListView)findViewById(R.id.lvJoke);
	        lvJoke.setOnItemClickListener(this);
	        
	        lvJoke.setOnScrollListener(this);
	        lraJoke = new ListRowAdapterSearch(this);

	        btnSearch.setOnClickListener(this);
	        
	        lvJoke.setAdapter(lraJoke);
	        
	        init();
	 }
	 
	 @Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		init();
	}
	 
	public void init(){
		 lraJoke.setList(model.getListJoke());
	}


	@Override
	public void onClick(View v) {
		
		if(btnSearch.equals(v)){
			model.clear();
			model.readAll();
			
			myDebug.v("searchcontroller");

				lraJoke.setList(model.getListJoke());
		
		}
		myDebug.v("fertig");
		lvJoke.setAdapter(lraJoke);
	
	}
	

	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		// TODO Auto-generated method stub
		
		List<Joke> listJoke = model.getListJoke();
		
		//oneline?
		if(position == listJoke.size()){
			return;
		}
	
		Joke joke = listJoke.get(position);
		

		
		Intent intent = new Intent(this, JokeController.class);
		intent.putExtra("JOKE", joke);
	
		startActivity(intent);
		
	}
	
	

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

		this.firstVisibleItem = firstVisibleItem; 
		this.visibleItemCount = visibleItemCount;
		this.totalItemCount   = totalItemCount;
		

	}
	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
	    if(scrollState == SCROLL_STATE_IDLE){
			int lastInScreen = firstVisibleItem + visibleItemCount;    
			   if((lastInScreen == totalItemCount)){     
				   isScrollCompleted();
			   }
	    	
	    }

	 }
	
	private void isScrollCompleted() {
		
		if(!model.isLoading() && model.isStoredMore()){
			setLoading(true);
			model.readAll();
		}

	}
	
	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		super.update(observable, data);
		
		myDebug.v("con add: " + model.getListJoke().size());

        lraJoke.setList(model.getListJoke());  
        
        if(model.isStoredMore()){
        	lraJoke.addEndItem(new OneLineView(this,R.string.loadmore));
        }else{
        	lraJoke.addEndItem(new OneLineView(this,R.string.nomore));
        }
        
        lvJoke.setAdapter(lraJoke);
		
	}
	 
}
